<?php

namespace App\Models;

use App\Models\Field\CommentField;

/**
 * App\Models\SysComment
 * This is the model class for table "sys_comment_".
 *
 * @property int $id 自增长ID
 * @property string $comment_id 评论ID
 * @property string $user_id 用户ID
 * @property string $target_id 评论的目标ID
 * @property int $target_type 评论目标类型：1笔记，2动态
 * @property int $note_type 主体的具体类型，等于对应主体表中的type字段。如：主体是笔记（此笔记是随手记），那么这个字段的含义就是【随手记】
 * @property string $content 内容
 * @property int $is_deleted 删除状态，0正常，1删除
 * @property int $is_view 是否可见，0不可见，1可见。当评论被删除，但其下有回复时，此时评论是可见的
 * @property int $is_shield 是否屏蔽，0正常，1屏蔽
 * @property int $created_at 创建时间戳
 * @property int $updated_at 更新时间戳
 * @property int $hot 热度，点赞+1
 * @property int $have_author_reply 是否有作者回复，0否1是
 * @method static \Illuminate\Database\Eloquent\Builder|SysComment newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SysComment newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SysComment query()
 *
 * @property User $user
 * @property IndexComment $indexComment
 * @property SysNote $note
 * @property SysDynamic $dynamic
 */
class SysComment extends BaseModel
{
    protected $table = "sys_comment_";
    protected $primaryKey = "id";
    protected $keyType = "int";
    public $timestamps = true;

    protected $fillable = [];

    protected $hidden = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'timestamp',
        'updated_at' => 'timestamp',
    ];

    public $fields = [
        CommentField::SELF_LIST => ['id', 'comment_id', 'user_id', 'target_id', 'target_type', 'content', 'is_deleted', 'created_at'],
    ];

    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'user_id');

    }

    public function indexComment()
    {
        return $this->hasOne(IndexComment::class, 'comment_id', 'comment_id');
    }

    public function note()
    {
//        $relation = $this->getRelationAttributes();
//        return $this->hasOne(SysDynamic::class, 'dynamic_id', 'target_id', $relation->target_id);

        $tableName = SysNote::getTableNameById($this->target_id);
        SysNote::setTableName($tableName);
        return $this->hasOne(SysNote::class, 'note_id', 'target_id');
    }

    public function dynamic()
    {
        $indexDynamic = IndexDynamic::getInstance()
            ->select(['user_id'])
            ->where('dynamic_id', $this->target_id)
            ->first();
        $tableName = SysDynamic::getTableNameById($indexDynamic->user_id);
        SysDynamic::setTableName($tableName);
        return $this->hasOne(SysDynamic::class, 'dynamic_id', 'target_id');
    }

    public function getDynamicAttribute()
    {
        $indexDynamic = IndexDynamic::getInstance()
            ->select(['user_id'])
            ->where('dynamic_id', $this->target_id)
            ->first();
        if (empty($indexDynamic)) return null;
        return SysDynamic::getModel($indexDynamic->user_id)->where('dynamic_id', $this->target_id)->first();
    }

    public function commonFormat($scene = CommentField::BASE_INFO): array
    {
        $formats = CommentField::getFormats($scene);
        return CommentField::format($this, $formats);
    }
}
